package com.demo.androidgame.finsternis;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

import com.demo.androidgame.fx.Game;
import com.demo.androidgame.fx.Input.KeyEvent;
import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.gl.Texture;
import com.demo.androidgame.fx.gl.TextureRegion;
import com.demo.androidgame.fx.impl.GLScreen;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Rectangle;
import com.demo.androidgame.fx.math.Vector2;

public class StageScreen extends GLScreen {
    Camera2D guiCam;
    SpriteBatcher batcher;
    Rectangle back;
    Rectangle stagenum1, stagenum2, stagenum3, 
    		stagenum4, stagenum5, stagenum6, 
    		stagenum7, stagenum8, stagenum9;
    Vector2 touchPoint;
    Texture stagemap;			//!
    TextureRegion stagemapRegion;  //! 
    
    public StageScreen(Game game) {
        super(game);
        
        guiCam = new Camera2D(glGraphics, 480, 320);
        back = new Rectangle(240-32, 320-40-32, 64, 64); //240, 320-40, 48, 48 
        stagenum1 = new Rectangle(60-32, 60-32, 64, 64);
        stagenum2 = new Rectangle(60-32, 150-32, 64, 64);
        stagenum3 = new Rectangle(150-32, 150-32, 64, 64);
        stagenum4 = new Rectangle(150-32, 60-32, 64, 64);
        stagenum5 = new Rectangle(240-32, 60-32, 64, 64);
        stagenum6 = new Rectangle(240-32, 150-32, 64, 64);
        stagenum7 = new Rectangle(330-32, 150-32, 64, 64);
        stagenum8 = new Rectangle(330-32, 60-32, 64, 64);
        stagenum9 = new Rectangle(420-32, 60-32, 64, 64);
        touchPoint = new Vector2();
        batcher = new SpriteBatcher(glGraphics, 100);
        
    }
    
    @Override
    public void resume() {
    	stagemap = new Texture(glGame, "stagemap1.png" );					//!
    	stagemapRegion = new TextureRegion(stagemap, 0, 0, 500, 350);		//!
    }
    
    @Override
    public void pause() {
    	stagemap.dispose();	//!
    }

    @Override
    public void update(float deltaTime) {
    	//backButton
    	List<KeyEvent> keyEvents = game.getInput().getKeyEvents();
        int key = keyEvents.size();
        for (int i = 0; i <key; i++) {
        	KeyEvent event2 = keyEvents.get(i);
        	if (event2.type == KeyEvent.KEY_DOWN){	
	        	if (event2.keyCode == android.view.KeyEvent.KEYCODE_BACK) {
	        		((Activity)game).runOnUiThread(new Runnable() {
						@Override
						public void run() {
							new AlertDialog.Builder((Activity)game)
		        			.setIcon(R.drawable.finsternis)
		        			.setTitle("Finsternis")
		        			.setMessage("게임을 종료하시겠습니까?")
		        			.setCancelable(true)
		        			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									((Activity)game).finish();
								}
		        			}).setNegativeButton("No", null).show();
						}
					});
	        	}
        	}
        }
    	
    	
    	
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
                
            //스테이지 선택
            if(event.type == TouchEvent.TOUCH_UP) {
            	touchPoint.set(event.x, event.y);
                guiCam.touchToWorld(touchPoint);
                
            	//backbutton
                if(OverlapTester.pointInRectangle(back, touchPoint)) {
                	Assets.playSound(Assets.clickSound);
                    game.setScreen(new MainMenuScreen(game));
                    return;
                }
                //stage1
                if(OverlapTester.pointInRectangle(stagenum1, touchPoint)) {
                	Assets.playSound(Assets.clickSound);
                	World.stage = 1;
                	GameScreen.bobStartX = 1;
                	GameScreen.bobStartY = 1;
                    game.setScreen(new GameScreen(game));
                    return;
                }
                //stage2
                if(OverlapTester.pointInRectangle(stagenum2, touchPoint)) {
                	if (Settings.stageActive[1] == true){
	                	Assets.playSound(Assets.clickSound);
	                	World.stage = 2;
	                	GameScreen.bobStartX = 1;
	                	GameScreen.bobStartY = 1;
	                    game.setScreen(new GameScreen(game));
	                    return;
                	}
                }
                
                //stage3
                if(OverlapTester.pointInRectangle(stagenum3, touchPoint)) {
                	if (Settings.stageActive[2] == true){
	                	Assets.playSound(Assets.clickSound);
	                	World.stage = 3;
	                	GameScreen.bobStartX = 1;
	                	GameScreen.bobStartY = 1;
	                    game.setScreen(new GameScreen(game));
	                    return;
                	}
                }

                //stage4
                if(OverlapTester.pointInRectangle(stagenum4, touchPoint)) {
                	if (Settings.stageActive[3] == true){
	                	Assets.playSound(Assets.clickSound);
	                	World.stage = 4;
	                	GameScreen.bobStartX = 1;
	                	GameScreen.bobStartY = 6;
	                    game.setScreen(new GameScreen(game));
	                    return;
                	}
                }
                
                //stage5
                if(OverlapTester.pointInRectangle(stagenum5, touchPoint)) {
                	if (Settings.stageActive[4] == true){
	                	Assets.playSound(Assets.clickSound);
	                	World.stage = 5;
	                	GameScreen.bobStartX = 31;
	                	GameScreen.bobStartY = 1;
	                    game.setScreen(new GameScreen(game));
	                    return;
                	}
                }
                
                //stage6
                if(OverlapTester.pointInRectangle(stagenum6, touchPoint)) {
                	if (Settings.stageActive[5] == true){
	                	Assets.playSound(Assets.clickSound);
	                	World.stage = 6;
	                	GameScreen.bobStartX = 29;
	                	GameScreen.bobStartY = 9;
	                    game.setScreen(new GameScreen(game));
	                    return;
                	}
                }
            }
        }
    }

    @Override
    public void present(float deltaTime) {
        GL10 gl = glGraphics.getGL();        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();
        
        gl.glEnable(GL10.GL_TEXTURE_2D);
        
        batcher.beginBatch(stagemap);			//!
        batcher.drawSprite(240, 160, 480, 320, stagemapRegion);	//!
        batcher.endBatch();
        
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        
        batcher.beginBatch(Assets.stagenumber);          
        batcher.drawSprite(240, 320-40, 48, 48, Assets.back);
        batcher.drawSprite(60, 60, 48, 48, Assets.stagenum1);
        batcher.drawSprite(60, 150, 48, 48, Assets.stagenum2);
        batcher.drawSprite(150, 150, 48, 48, Assets.stagenum3);
        batcher.drawSprite(150, 60, 48, 48, Assets.stagenum4);
        batcher.drawSprite(240, 60, 48, 48, Assets.stagenum5);
        batcher.drawSprite(240, 150, 48, 48, Assets.stagenum6);
        batcher.drawSprite(330, 150, 48, 48, Assets.stagenum7);
        batcher.drawSprite(330, 60, 48, 48, Assets.stagenum8);
        batcher.drawSprite(420, 60, 48, 48, Assets.stagenum9);
        batcher.endBatch();
        
        gl.glDisable(GL10.GL_BLEND);
        gl.glDisable(GL10.GL_TEXTURE_2D);
        
        gl.glDisable(GL10.GL_LIGHT1);
		gl.glDisable(GL10.GL_LIGHTING);
    }

    @Override
    public void dispose() {
    }
}
